import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.net.Socket;
  
  public class HttpServiceChecker
 {
     public static void main(String[] args) throws Exception
     {
         boolean flag = isHttpOpen("www.baidu.com", 80);
         System.out.println(flag ? "HTTP服务启动..." : "HTTP服务关闭...");
     }
     /**
      * 判断ip的port端口是否开放了http服务
      */
     public static boolean isHttpOpen(String ip, int port)
     {
         if(isEmpty(ip))
         {
             return false;
         }
         Socket socket = null;        // socket链接
         OutputStream os = null;      // 输出流
         BufferedReader br = null;    // 输入流
         boolean flag = false;        // 是否开启了HTTP服务
         try
         {
             socket = new Socket(ip, port);
             os = socket.getOutputStream();
             br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             // 创建HTTP请求报文信息
             String reqInfo =  "HEAD / HTTP/1.0"
                             + "\r\n"
                             + "Host: " + ip + ":" + port
                             + "\r\n\r\n";
             System.out.println("请求报文 : \r\n" + reqInfo);
             // 发送请求消息
             os.write(reqInfo.getBytes());
             os.flush();
             // 接收响应消息
             String lineStr = null;
           System.out.println("响应报文 : ");
             while((lineStr = br.readLine()) != null)
             {
                 // 读取到了响应信息，表示该ip的port端口提供了HTTP服务
                 System.out.println(lineStr);
                 flag = true;
             }
         } catch (Exception e)
         {
             e.printStackTrace();
         }
         finally
         {
             try
             {
                 if(br != null)
                     br.close();
                 if(os != null)
                     os.close();
                 if(socket != null)
                     socket.close();
             } catch (IOException e)
             {
                 e.printStackTrace();
             }
         }
         return flag;
     }
     
     private static boolean isEmpty(String str)
     {
         if(str == null || str.length() <= 0 || str.trim() == null || str.trim().length() <= 0)
         {
             return true;
         }
         return false;
     }
 }